package com.cencat.framework.file.util;

import lombok.experimental.UtilityClass;
import org.springframework.util.StringUtils;

import java.util.HashMap;
import java.util.Map;

/**
 * 文件类型工具类
 */
@UtilityClass
public class FileTypeUtil {
    
    private static final Map<String, String> MIME_TYPE_MAP = new HashMap<>();
    private static final Map<String, String> FILE_ICON_MAP = new HashMap<>();
    
    static {
        // 初始化MIME类型映射
        MIME_TYPE_MAP.put("jpg", "image/jpeg");
        MIME_TYPE_MAP.put("jpeg", "image/jpeg");
        MIME_TYPE_MAP.put("png", "image/png");
        MIME_TYPE_MAP.put("gif", "image/gif");
        MIME_TYPE_MAP.put("bmp", "image/bmp");
        MIME_TYPE_MAP.put("pdf", "application/pdf");
        MIME_TYPE_MAP.put("doc", "application/msword");
        MIME_TYPE_MAP.put("docx", "application/vnd.openxmlformats-officedocument.wordprocessingml.document");
        MIME_TYPE_MAP.put("xls", "application/vnd.ms-excel");
        MIME_TYPE_MAP.put("xlsx", "application/vnd.openxmlformats-officedocument.spreadsheetml.sheet");
        MIME_TYPE_MAP.put("ppt", "application/vnd.ms-powerpoint");
        MIME_TYPE_MAP.put("pptx", "application/vnd.openxmlformats-officedocument.presentationml.presentation");
        MIME_TYPE_MAP.put("txt", "text/plain");
        MIME_TYPE_MAP.put("csv", "text/csv");
        MIME_TYPE_MAP.put("mp4", "video/mp4");
        MIME_TYPE_MAP.put("avi", "video/x-msvideo");
        MIME_TYPE_MAP.put("mov", "video/quicktime");
        MIME_TYPE_MAP.put("mp3", "audio/mpeg");
        MIME_TYPE_MAP.put("wav", "audio/wav");
        MIME_TYPE_MAP.put("zip", "application/zip");
        MIME_TYPE_MAP.put("rar", "application/x-rar-compressed");
        
        // 初始化文件图标映射
        FILE_ICON_MAP.put("image", "file-image");
        FILE_ICON_MAP.put("video", "file-video");
        FILE_ICON_MAP.put("audio", "file-audio");
        FILE_ICON_MAP.put("pdf", "file-pdf");
        FILE_ICON_MAP.put("word", "file-word");
        FILE_ICON_MAP.put("excel", "file-excel");
        FILE_ICON_MAP.put("powerpoint", "file-powerpoint");
        FILE_ICON_MAP.put("archive", "file-archive");
        FILE_ICON_MAP.put("text", "file-text");
        FILE_ICON_MAP.put("unknown", "file-unknown");
    }
    
    /**
     * 根据文件名获取文件类型
     */
    public static String getFileType(String filename) {
        if (!StringUtils.hasText(filename)) {
            return "unknown";
        }
        
        String extension = getFileExtension(filename).toLowerCase();
        
        if (extension.matches("(jpg|jpeg|png|gif|bmp|webp|svg)")) {
            return "image";
        } else if (extension.matches("(mp4|avi|mov|wmv|flv|mkv|webm)")) {
            return "video";
        } else if (extension.matches("(mp3|wav|flac|aac|ogg)")) {
            return "audio";
        } else if (extension.equals("pdf")) {
            return "pdf";
        } else if (extension.matches("(doc|docx)")) {
            return "word";
        } else if (extension.matches("(xls|xlsx)")) {
            return "excel";
        } else if (extension.matches("(ppt|pptx)")) {
            return "powerpoint";
        } else if (extension.matches("(zip|rar|7z|tar|gz)")) {
            return "archive";
        } else if (extension.matches("(txt|log|csv|xml|json|yml|yaml|md)")) {
            return "text";
        } else {
            return "unknown";
        }
    }
    
    /**
     * 根据文件扩展名获取MIME类型
     */
    public static String getMimeType(String filename) {
        String extension = getFileExtension(filename).toLowerCase();
        return MIME_TYPE_MAP.getOrDefault(extension, "application/octet-stream");
    }
    
    /**
     * 获取文件扩展名
     */
    public static String getFileExtension(String filename) {
        if (!StringUtils.hasText(filename)) {
            return "";
        }
        
        int dotIndex = filename.lastIndexOf(".");
        return dotIndex > 0 ? filename.substring(dotIndex + 1) : "";
    }
    
    /**
     * 获取文件图标
     */
    public static String getFileIcon(String fileType) {
        return FILE_ICON_MAP.getOrDefault(fileType, "file-unknown");
    }
    
    /**
     * 格式化文件大小
     */
    public static String formatFileSize(long fileSize) {
        if (fileSize < 1024) {
            return fileSize + " B";
        } else if (fileSize < 1024 * 1024) {
            return String.format("%.2f KB", fileSize / 1024.0);
        } else if (fileSize < 1024 * 1024 * 1024) {
            return String.format("%.2f MB", fileSize / (1024.0 * 1024.0));
        } else {
            return String.format("%.2f GB", fileSize / (1024.0 * 1024.0 * 1024.0));
        }
    }
    
    /**
     * 检查文件类型是否允许上传
     */
    public static boolean isAllowedFileType(String filename, String[] allowedTypes) {
        String extension = getFileExtension(filename).toLowerCase();
        for (String allowedType : allowedTypes) {
            if (allowedType.equalsIgnoreCase(extension)) {
                return true;
            }
        }
        return false;
    }
}